<?php
declare(strict_types=1);

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Logic\Role as RoleLogic;
use App\Logic\Menu as MenuLogic;
use App\Logic\Account as AccountLogic;
use App\Http\Requests\Role as RoleRequest;
use Illuminate\Support\Facades\Validator;

class RoleController extends Controller
{
    private $roleLogic;
    private $roleRequest;
    private $menuLogic;
    private $accountLogic;

    public function __construct(){
        $this->roleLogic = new RoleLogic();
        $this->roleRequest = new RoleRequest();
        $this->menuLogic = new MenuLogic();
        $this->accountLogic = new AccountLogic();
    }

    public function index(){
        return view('role.index');
    }

    public function list(Request $request)
    {
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $start = $page * $limit - $limit;
        $where['role_name'] = $request->input('name','');
        $result = [
            'code' => 0,
            'msg' => '获取数据成功',
            'count' => $this->roleLogic->count($where),
            'data' =>  $this->roleLogic->list((int)$start, (int)$limit, $where),
        ];
        return response()->json($result);
    }

    public function add(Request $request)
    {
        if ($request->isMethod('POST')) {
            $result = ['status'=>false, 'msg'=>'新增失败！'];
            $data['role_name']= $request->post('role_name','');
            $data['role_intro']= $request->post('role_intro','');
            $data['role_sort']= $request->post('role_sort',0);
            $data['auth']= $request->post('auth');

            $validator = Validator::make($request->all(), $this->roleRequest->rules(), $this->roleRequest->messages());
            if ($validator->fails()) {
                $result['msg'] = $validator->errors()->first();
                return response()->json($result);
            }

            if(!$this->roleLogic->checkName($data['role_name'])){
                $result['msg'] = '角色名已经存在！';
                return response()->json($result);
            }

            if ($this->roleLogic->add($data)){
                $result['status'] = true;
                $result['msg'] = '新增成功！';
                return response()->json($result);
            }
            return response()->json($result);

        } else {
            $subArr = $this->menuLogic->sub2arr();
            return view('role.add', ['menu' => $subArr]);
        }
    }

    public function edit(Request $request, $id = 0)
    {
        if ($request->isMethod('POST')) {
            $result = ['status'=>false, 'msg'=>'修改失败！'];

            $id = $request->post('id',0);
            $data['role_name']= $request->post('role_name','');
            $data['role_intro']= $request->post('role_intro','');
            $data['role_sort']= $request->post('role_sort',0);
            $data['auth'] = $request->post('auth','');

            if(($id < 1) || !($this->roleLogic->has((int)$id))){
                $result['msg'] = '请勿非法提交！';
                return response()->json($result);
            }

            if(!$this->roleLogic->checkName($data['role_name'],(int)$id)){
                $result['msg'] = '角色名已经存在！';
                return response()->json($result);
            }

            $validator = Validator::make($request->all(), $this->roleRequest->rules(), $this->roleRequest->messages());
            if ($validator->fails()) {
                $result['msg'] = $validator->errors()->first();
                return response()->json($result);
            }

            if ($this->roleLogic->update((int)$id, $data)){
                $result['status'] = true;
                $result['msg'] = '修改成功！';
                return response()->json($result);
            }

            return response()->json($result);
        } else {
            if ($id < 1) abort(404);
            $data = $this->roleLogic->findToArray((int)$id);
            if (empty($data)) abort(404);
            $subArr = $this->menuLogic->sub2arr();
            return view('role.edit', ['data' => $data, 'menu' => $subArr]);
        }
    }

    public function delete(Request $request, $id = 0)
    {
        $result = ['status'=>false, 'msg'=>'删除失败！'];
        if(($id < 1) || !($this->roleLogic->has((int)$id))){
            $result['msg'] = '请勿非法提交！';
            return response()->json($result);
        }
        if ($id == 1){
            $result['msg'] = '本角色禁止删除！';
            return response()->json($result);
        }
        $accountCount = $this->accountLogic->countByRoleId((int)$id);
        if ($accountCount > 0) {
            $result['msg'] = '该角色下有管理员，不能删除！';
            return response()->json($result);
        }
        if ($this->roleLogic->delete((int)$id)){
            $result['status'] = true;
            $result['msg'] = '删除成功！';
            return response()->json($result);
        }
        return response()->json($result);
    }
}
